<?php
class db_beta
{
	function __construct()
	{
		Global $mySql;
		$this->mySql = $mySql;
	}
	private function createBetaCode($data)
	{
		$sql = "INSERT INTO beta (code, invites) VALUES ('%s','%s')";
		$query = sprintf($sql,
			mysql_real_escape_string($data[code]),
			mysql_real_escape_string($data[invites]));
		$this->mySql->makeQuery($query);
	}
	public function checkBetaCode($code)
	{
		$sql = "SELECT * FROM beta WHERE code='%s'";
		$query = sprintf($sql,
			mysql_real_escape_string($code));
		$result = $this->mySql->makeQuery($query);
		$rows = mysqli_num_rows($result);
		if($rows == 1)
		{
			$temp = mysqli_fetch_array($result);
			if($temp[invites] > "0")
			{
				$beta[id] = $temp[id];
				$beta[code] = $temp[code];
				$beta[invites] = $temp[invites];				
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	public function useBetaCode($code)
	{
		$sql = "SELECT * FROM beta WHERE code='%s'";
		$query = sprintf($sql,
			mysql_real_escape_string($code));
		$result = $this->mySql->makeQuery($query);
		$rows = mysqli_num_rows($result);
		if($rows == 1)
		{
			$temp = mysqli_fetch_array($result);
			if($temp[invites] > "0")
			{
				$beta[id] = $temp[id];
				$beta[code] = $temp[code];
				$beta[invites] = $temp[invites];
				--$beta[invites];
				$sql = "UPDATE beta SET invites= %d WHERE id= %d";

				$query = sprintf($sql,mysql_real_escape_string($beta[invites]), mysql_real_escape_string($temp[id]));
				$result = $this->mySql->makeQuery($query);
				
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
}
?>